/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainFxybdx;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainFxybdxService;
import me.zhengjie.domain.vo.TrainFxybdxQueryCriteria;
import me.zhengjie.mapper.TrainFxybdxMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-06
**/
@Service
@RequiredArgsConstructor
public class TrainFxybdxServiceImpl extends ServiceImpl<TrainFxybdxMapper, TrainFxybdx> implements TrainFxybdxService {

    private final TrainFxybdxMapper trainFxybdxMapper;

    @Override
    public TrainFxybdx findOne(Long trainId, Long trainItemId) {
        return trainFxybdxMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainFxybdx> queryAll(TrainFxybdxQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainFxybdxMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainFxybdx> queryAll(TrainFxybdxQueryCriteria criteria){
        return trainFxybdxMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainFxybdx resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainFxybdx resources) {
        TrainFxybdx trainFxybdx = getById(resources.getId());
        trainFxybdx.copy(resources);
        saveOrUpdate(trainFxybdx);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainFxybdx> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainFxybdx trainFxybdx : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练ID", trainFxybdx.getTrainId());
            map.put("平视仪复杂动作判断", trainFxybdx.getPsydz());
            map.put("仪表复杂动作判断", trainFxybdx.getYbdz());
            map.put("自由练习", trainFxybdx.getZylx());
            map.put("固定航线飞行", trainFxybdx.getGdhxfx());
            map.put("模拟飞行着陆", trainFxybdx.getMnfxzl());
            map.put("速度表闪现", trainFxybdx.getSdbsx());
            map.put("高度表闪现", trainFxybdx.getGdbsx());
            map.put("平视仪闪现", trainFxybdx.getPsysx());
            map.put("地平仪闪现", trainFxybdx.getDpysx());
            map.put("罗盘闪现", trainFxybdx.getLpsx());
            map.put("坚信平视仪_航向误差", trainFxybdx.getJxpsyHxwc());
            map.put("坚信平视仪_高度误差", trainFxybdx.getJxpsyGdwc());
            map.put("坚信平视仪_附加成绩", trainFxybdx.getJxpsyFjcj());
            map.put("坚信平视仪_保持时间", trainFxybdx.getJxpsyBcsj());
            map.put("坚信仪表_航向误差", trainFxybdx.getJxybHxwc());
            map.put("坚信仪表_高度误差", trainFxybdx.getJxybGdwc());
            map.put("坚信仪表_附加成绩", trainFxybdx.getJxybFjcj());
            map.put("坚信仪表_保持时间", trainFxybdx.getJxybBcsj());
            map.put("评定结果", trainFxybdx.getResult());
            map.put("记录人", trainFxybdx.getCreateBy());
            map.put("创建时间", trainFxybdx.getCreateTime());
            map.put("记录日期", trainFxybdx.getDate());
            map.put("更新时间", trainFxybdx.getUpdateTime());
            map.put("训练项目ID", trainFxybdx.getTrainItemId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}